<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes"/>
<head>
    <meta charset="UTF-8">
    <title>免费申领</title>
</head>
<link type="text/css" rel="stylesheet"  th:href="@{/css/admin/bootstrap/css/bootstrap.css}" >
<link type="text/css" rel="stylesheet"  th:href="@{/css/sweetalert.css}" >
<script th:src="@{/js/sweetalert-dev.js}"></script>
<script th:src="@{/js/alipayjsapi.js}"></script>
<script th:src="@{/js/jquery-1.11.3.min.js}"></script>
<body style="background-color: #f2f1f7">
<div style="text-align: center;overflow: hidden;background-color: #fff">
    <img th:src="@{/images/banner.jpeg}" alt="" style="width: 100%">
</div>
<div style="margin-top: 1px;">
    <ul class="list-group">
        <li class="list-group-item" style="border: 0;padding: 20px 10px">
            <span style="float: right; color: #00cdb7">芝麻分600及以上可免押金</span>
            免押条件
        </li>
        <li class="list-group-item" style="border: 0;padding: 20px 10px">
            <span style="float: right; color: darkgray"><span
                    style="color: #00cdb7;border: 1px solid aquamarine;padding: 2px;margin-right: 2px;">免押金</span><del id="delAmount"></del></span>
            押金
        </li>
    </ul>
</div>
<form style="padding: 0 3%">
    <div class="checkbox">
        <label>
            <input type="checkbox" name="notice" checked> 我同意商户查询我的芝麻分或评估结果
        </label>
    </div>
    <span style="margin-bottom: 3px;font-size: 12px;">注：本电子设备寄出日起30天内，若无注册成功并使用一次，将会在支付宝中扣取<span id="payAmount"></span>元设备款，如已注册使用，则不扣。</span><br/>
    <div style="margin-top: 10px">
        <button id="J_btn" class="btn btn-success btn-block btn-lg" value="auth" type="button">提交申请</button>
    </div>
</form>
<script>
    const price = window.sessionStorage.getItem("productPrice");
    const btn = document.querySelector('#J_btn');
    const notice = document.getElementsByName('notice');
    document.getElementById('payAmount').innerText = '' + price;
    document.getElementById('delAmount').innerText = '' + price + '元';
    btn.addEventListener('click', function () {
        var bId = this.value;
        $("#J_btn").attr("disabled","true");
        if (notice[0].checked) {
            let receiveName = window.sessionStorage.getItem("receiveName"),
                receiveMobile = window.sessionStorage.getItem("receiveMobile"),
                receiveAddress = window.sessionStorage.getItem("receiveAddress"),
                uid = window.sessionStorage.getItem("uid"),
                pid = window.sessionStorage.getItem("pid");

            if(receiveName === undefined || receiveName === "" || receiveName === null
                || receiveMobile === undefined || receiveMobile === "" || receiveMobile === null
                || receiveAddress === undefined || receiveAddress === "" || receiveAddress === null
                || uid === undefined || uid === "" || uid === null
                || pid === undefined || pid === "" || pid === null
            ){
                ap.alert("信息失效，请重新扫码！");
                return;
            }
            var params = {
                bId : bId,
                receiveName : receiveName,
                receiveMobile : receiveMobile,
                receiveAddress : receiveAddress,
                uid : uid,
                pid : pid
            }
            var orderStr = "";
            var seqNo = "";
            $.ajax({
                url: "/pay/credit/alipay/auth",
                type: "post",
                dataType: "json",
                data:params,
                cache: false,
                async: false,
                success: function(data) {
                    if(data.code === "00000000"){
                        orderStr = data.data.orderStr;
                        seqNo =  data.data.seqOrderNo;
                    }else{
                        ap.alert(data.msg);
                    }
                }
            })
            if(orderStr !== undefined && orderStr !== "" && orderStr !== null){
                ap.tradePay({
                    orderStr: orderStr
                }, function (res) {
                    var paramsRes = {
                        seqNo : seqNo
                    }
                    if (res.resultCode === "9000" ||res.resultCode === "8000") {
                      if(res.resultCode === "8000"){
                          $.ajax({
                              url: "/pay/credit/order/complete",
                              type: "post",
                              dataType: "json",
                              data:paramsRes,
                              cache: false,
                              async: true,
                              success: function(data) {
                              }
                          });
                      }
                        window.sessionStorage.removeItem("receiveName");
                        window.sessionStorage.removeItem("receiveMobile");
                        window.sessionStorage.removeItem("receiveAddress");
                        window.sessionStorage.removeItem("uid");
                        window.sessionStorage.removeItem("pid");
                        location.href = "complete.html";
                    }else {
                        $.ajax({
                            url: "/pay/credit/order/fail",
                            type: "post",
                            dataType: "json",
                            data:paramsRes,
                            cache: false,
                            async: true,
                            success: function(data) {
                            }
                        });
                    }
                    if (res.resultCode === "4000") {
                        ap.alert("订单支付失败");
                        $("#J_btn").removeAttr("disabled");
                    }
                    if (res.resultCode === "6001") {
                        if(bId ==="auth"){
                            swal({
                                    title: "需要用预支付申请？",
                                    text: "信用分不够，没关系，可申请预支出一笔费用，交易未达成可原路退回你的账户",
                                    type: "warning",
                                    showCancelButton: true,
                                    confirmButtonColor: "#DD6B55",
                                    confirmButtonText: "用预支付申请",
                                    cancelButtonText: "取消",
                                    closeOnConfirm: true,
                                    closeOnCancel: true
                                },
                                function(isConfirm){
                                    if (isConfirm) {
                                        document.getElementById('J_btn').value='trade';
                                        document.getElementById('J_btn').innerText = '预支付申请';
                                    }
                                });
                            $("#J_btn").removeAttr("disabled");
                        }else {
                            ap.alert("已取消支付");
                            $("#J_btn").removeAttr("disabled");
                        }
                    }
                    if (res.resultCode === "6002") {
                        ap.alert("网络连接出错");
                        $("#J_btn").removeAttr("disabled");
                    }
                });
            }else{
                swal("拼接参数时出错");
                $("#J_btn").removeAttr("disabled");
            }
        } else {
            swal("请勾选服务协议");
            $("#J_btn").removeAttr("disabled");
        }
    });
</script>
</body>
</html>